package com.mz361.modules.system.service.impl;


import com.mz361.common.data.PageSort;
import com.mz361.common.enums.StatusEnum;
import com.mz361.common.utils.DateUtils;
import com.mz361.modules.system.domain.PayOrder;
import com.mz361.modules.system.repository.OrderRepository;
import com.mz361.modules.system.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liaomch
 * @date 2024/05/07
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    /**
     * 根据ID查询数据
     *
     * @param id 主键ID
     */
    @Override
    public PayOrder getById(Long id) {
        return orderRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     *
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<PayOrder> getPageList(Example<PayOrder> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return orderRepository.findAll(example, page);
    }

    @Override
    @Transactional
    public void delNopayList() {
        // 使用Specification复杂查询
        List<PayOrder> payOrders = orderRepository.findAll(new Specification<PayOrder>() {
            @Override
            public Predicate toPredicate(Root<PayOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> preList = new ArrayList<>();
                Date specificTime = DateUtils.getDayHourAfter(new Date(), -1);
                Predicate datePredicate = cb.lessThan(root.get("createDate"), specificTime);
                preList.add(datePredicate);
                Predicate resultStatus = cb.equal(root.get("resultStatus"), "NOTPAY");
                preList.add(resultStatus);
// 定义第二个条件，例如状态等于某个特定值
                byte status = 3; // 假设有一个状态字段
                Predicate statusPredicate = cb.notEqual(root.get("status").as(Byte.class), status);
                preList.add(statusPredicate);
                Predicate[] pres = new Predicate[preList.size()];
// 将组合的条件添加到查询中
                return query.where(preList.toArray(pres)).getRestriction();
            }

        });
        orderRepository.deleteAll(payOrders);
    }

    /**
     * 保存数据
     *
     * @param payOrder 实体对象
     */
    @Override
    public PayOrder save(PayOrder payOrder) {
        return orderRepository.save(payOrder);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return orderRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public Long countAll() {
        return orderRepository.countAll();
    }
}